package vn.com.shoppie.object;

import java.util.ArrayList;

import vn.com.shoppie.activity.MainActivity;
import android.app.AlertDialog;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.drawable.Drawable;
import android.util.FloatMath;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapView;
import com.google.android.maps.OverlayItem;
import com.google.android.maps.Projection;

@SuppressWarnings("rawtypes")
public class MyPositionOverlay extends ItemizedOverlay {
	private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
	Context mContext;

	Drawable drawable = null;
	GeoPoint location;
	float radius = 0;
	private Paint paint1, paint2;

	public MyPositionOverlay(Context context, Drawable defaultMarker) {
		super(boundCenterBottom(defaultMarker));
		mContext = context;
		this.drawable = defaultMarker;

		
	}

	public int getHeight() throws NullPointerException {
		if (drawable == null || drawable.getBounds() == null)
			throw new NullPointerException("drawable null");

		int height = drawable.getBounds().height();
		return height;
	}

	public MyPositionOverlay(Context context, Drawable defaultMarker, GeoPoint location, float radius) {
		super(boundCenterBottom(defaultMarker));
		mContext = context;
		// this.drawable = defaultMarker;
		this.location = location;
		this.radius = radius;
	}

	@Override
	protected boolean onTap(int index) {
		OverlayItem item = mOverlays.get(index);
		AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);
		dialog.setTitle(item.getTitle());
		dialog.setMessage(item.getSnippet());
		dialog.show();
		return true;
	}

	@Override
	protected OverlayItem createItem(int i) {
		return mOverlays.get(i);
	}

	@Override
	public int size() {
		return mOverlays.size();
	}

	public void addOverlay(OverlayItem overlay) {
		mOverlays.add(overlay);
		populate();
	}

	public void removeAllOverlay() {
		mOverlays.removeAll(mOverlays);
		populate();
	}

	public void setOverlay(OverlayItem overlay) {
		removeAllOverlay();
		addOverlay(overlay);
	}

	public void setRadius(float radius) {
		this.radius = radius;
	}

	public void setMyLocation(GeoPoint geopoint) {
		this.location = geopoint;
	}

	@Override
	public void draw(Canvas canvas, MapView mapView, boolean shadow) {
		super.draw(canvas, mapView, false);
		if (!shadow) {
			// super.draw(canvas, mapView, false);
		} else {
			return;
		}
		if (radius > 0) {
//			Log.e("circle radius", radius+"m");
			int height = 12;
			try {
				height = getHeight();
			} catch (NullPointerException ex) {
				// Log.e("Null Height", ex.getMessage());
			}
			;

			Point pt = new Point();

			 Projection projection = mapView.getProjection();
			 projection.toPixels(MainActivity.myLocation, pt);
			 float circleRadius = projection.metersToEquatorPixels(radius) * (1 / FloatMath.cos((float) Math.toRadians(location.getLatitudeE6() / 1E6)));
//			 Log.e("circle radius", circleRadius+"px");
			 Paint innerCirclePaint;
			
			 // fill circle
			 innerCirclePaint = new Paint();
			 innerCirclePaint.setColor(Color.BLUE);
			 innerCirclePaint.setAlpha(25);
			 innerCirclePaint.setAntiAlias(true);
			 innerCirclePaint.setStyle(Paint.Style.FILL);
			 canvas.drawCircle((float) pt.x, (float) pt.y - height, circleRadius, innerCirclePaint);
			
			 // draw circle bound
			 innerCirclePaint = new Paint();
			 innerCirclePaint.setColor(Color.BLUE);
			 innerCirclePaint.setAlpha(50);
			 innerCirclePaint.setAntiAlias(true);
			 innerCirclePaint.setStyle(Paint.Style.STROKE);
			 canvas.drawCircle((float) pt.x, (float) pt.y - height,  circleRadius, innerCirclePaint);
			
//			 Log.e("Draw", "x="+pt.x+" y="+pt.y);

		}

	}
}
